#define UP 18432 
#define DOWN 20480 
#define LEFT 19200 
#define RIGHT 19712
#include"time.h" 
#include"stdlib.h"
#include"bios.h"
main()
{
      int input,snake[25][2],foodx,foody,a,b,c,snakeheadx=4,snakeheady=7,snakeend=2,aspect=0,needfood=0,playing=0;
      for (a=0;a!=25;++a)
      {
          snake[a][0]=14;
          snake[a][1]=9;
      } 
      snake[0][0]=4;snake[1][0]=5;snake[2][0]=6;
      snake[0][1]=7;snake[1][1]=7;snake[2][1]=7;
      do
      {
          if (needfood==0)
          {
                   do
                   {
                     foodx=rand_(1,12);
                     foody=rand_(1,7);   
                   }while (unfree(snake,foodx,foody)==0);
                   needfood=1;
          }
          system("cls");
          for (a=0;a!=9;++a) 
          {
              for (b=0;b!=14;++b)
              {
                  if (a==0 || a==8 || b==0 || b==13)
                     printf("Q");
                  else 
                  {
                       for (c=0;c!=25;++c)
                       {
                           if (a==snake[0][1] && b==snake[0][0])
                           {
                              cprintf("A"); 
                              goto P1;
                           }
                           else if (a==snake[c][1] && b==snake[c][0])
                           {
                              cprintf("X"); 
                              goto P1;
                           }
                       }
                       if (a==foody && b==foodx)
                                cprintf("Z"); 
                       else
                               cprintf("O"); 
                       P1:;
                  }
              }
              printf("\n");
          }
	  sleep(1);
          if(bioskey(1))
	  {
	      input=bioskey(0);
              switch (aspect) 
                    {
                           case 0: switch (input)
                                   {
                                          case UP: aspect=1;break;
                                          case DOWN: aspect=3;break;
                                          default: break;
                                   }
                                   break;
                           case 1: switch (input)
                                   {
                                          case LEFT: aspect=0;break;
                                          case RIGHT: aspect=2;break;
                                          default: break;
                                   }
                                   break;
                           case 2: switch (input)
                                   {
                                          case UP: aspect=1;break;
                                          case DOWN: aspect=3;break;
                                          default: break;
                                   }
                                   break;
                           case 3: switch (input)
                                   {
                                          case LEFT: aspect=0;break;
                                          case RIGHT: aspect=2;break;
                                          default: break;
                                   }
                                   break;
                    }
	  }
          switch (aspect)
          {
                 case 0:--snakeheadx;break; 
                 case 1:--snakeheady;break; 
                 case 2:++snakeheadx;break;
                 case 3:++snakeheady;break;
          }
          for (a=24;a!=0;--a) 
          {
              snake[a][0]=snake[a-1][0];
              snake[a][1]=snake[a-1][1];
          }
          snake[0][0]=snakeheadx;
          snake[0][1]=snakeheady;
          if (snake[0][0]==0 || snake[0][0]==13 || snake[0][1]==0 || snake[0][1]==8) 
          {
             playing=-1;
             goto P3;
          }
          else
          {
              for (a=1;a!=25;++a) 
              {
                  if (snake[0][0]==snake[a][0] && snake[0][1]==snake[a][1])
                  {
                                 playing=-1;
                                 goto P3;
                  }
              }
          }
          if (snake[0][0]!=foodx || snake[0][1]!=foody)
          {
               if (snakeend!=24)
                  {
                         snake[snakeend+1][0]=16;
                         snake[snakeend+1][1]=9;
                  }
          }
          else
          {
              ++snakeend;needfood=0; 
          }
          if (snakeend==24) 
             playing=1;
      }while (playing==0);
      P3: if (playing==-1)
         printf("GAME.OVER");
      else if (playing==1)
           printf("YOU.WIN");
      getch();
}
int unfree(int snake[25][2],int x,int y) 
{
    int a;
    for (a=0;a!=25;++a)
        if (x==snake[a][0] && y==snake[a][1]) 
           return 0;
    return -1;
}
int rand_(int a,int b) 
{ 
  int c,n; 
  if (a==b)
     n=a;
  else
  {
  if (a>b)
  {
          c=a;
          a=b;
          b=c;
  }
  srand(time(NULL)+rand()); 
  rand();
  b=32767/(++b-a); 
  n=rand()/b+a;
  if (n>b)
     n=b; 
  }
  return(n); 
}
